<!--
	This file is part of ELCube.
	ELCube is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	ELCube is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.
	You should have received a copy of the GNU Affero General Public License
	along with ELCube.  If not, see <https://www.gnu.org/licenses/>.
-->
<template>
    <span v-if="!editMode">
        <template v-if="value && value.length">{{value | formatCascader(options)}}</template>
        <template v-else-if="designMode">
            {{["zhejiang","hangzhou"] | formatCascader(options)}}
        </template>
        <span v-else class="empty"></span>
    </span>
    <a-cascader size="small"
              v-else
              v-model="val"
              :mode="inputOptions.selectMode||'default'"
              :options="options"
              :show-search="{ filter }"
              placeholder="请选择..."
              @change="change">
    </a-cascader>
</template>

<script>

const defaultOptions = [
    {
        "value": "zhejiang",
        "label": "浙江省",
        "children": [
            {
                "value": "hangzhou",
                "label": "杭州市",
                "children": [
                    {
                        "value": "xihu",
                        "label": "西湖区"
                    }
                ]
            }
        ]
    },
    {
        "value": "jiangsu",
        "label": "江苏省",
        "children": [
            {
                "value": "nanjing",
                "label": "南京市",
                "children": [
                    {
                        "value": "zhonghuamen",
                        "label": "中华门"
                    }
                ]
            }
        ]
    }
];

import {mapActions} from 'vuex';
export default {
    props:{
        editMode: Boolean,
        value: {},
        inputOptions: {
            type:Object,
            default(){
                return {}
            }
        },
        designMode: Boolean
    },
    filters:{
        formatCascader(value, options){
            if(value) {
                options = typeof options === 'string' ? JSON.parse(options) : options;
                let find = options;
                return value.map(item => {
                    find = find && find.find(o => o.key === item || o.value === item);
                    if (find) {
                        const label = find.label || find.name || find.title;
                        find = find.children;
                        return label;
                    }
                    return item;
                }).join(' / ');
            }
        },
    },
    data(){
        return {
            dictOptions: []
        }
    },
    created(){
        if(this.inputOptions.dictKey){
            this.json(this.inputOptions.dictKey)
                .then(data=>{
                    this.dictOptions = data;
                })
        }
    },
    computed:{
        options(){
            if(this.designMode){
                return defaultOptions;
            }
            if(this.inputOptions.dictKey){
                return this.dictOptions;
            }
            return this.inputOptions.optionsObject;
        },
        val:{
            get(){
                return this.value;
            },
            set(value){
                this.$emit('input',value);
            }
        }
    },
    methods:{
        ...mapActions('Dict',[
            'json'
        ]),
        change(){
            this.$emit('change',{});
        },
        filter(inputValue, path) {
            return path.some(option => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
        },
    }
}
</script>

<style scoped>

</style>